package use;

import swimming.DBUtils;
import swimming.consumption;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class consumeuse {
    public List<? extends Object> 增加(String []input)
    {try{
        List<consumption> list=new ArrayList<>();
        Connection con= DBUtils.getConnection();
        PreparedStatement st= con.prepareStatement("insert into 消费(电话,时间,人数) value(?,?,?)");
        for(int i=0;i< input.length;i++)
        {
            st.setString(i+1,input[i]);
        }
        st.executeUpdate();
        st= con.prepareStatement("select  * from 消费情况");
        ResultSet re=st.executeQuery();
        list=result(con,st,re);
        return list;}
    catch (SQLException E) {
        return null;
    }
    }
    public List<consumption> result(Connection con,PreparedStatement st,ResultSet re) throws SQLException
    {
        List<consumption> list=new ArrayList<>();
        try{
            while(re.next())
            {
                String depositamount=re.getString("存款金额");
                String balanceamount=re.getString("结存金额");
                String time=re.getString("时间");
                String cardnum=re.getString("电话");
                String consumeamount=re.getString("消费金额");
                consumption consume=new consumption(depositamount,cardnum,time,balanceamount,consumeamount);
                list.add(consume);
            }
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        finally {
            DBUtils.close(con,st,re);
        }
        return list;
    }
}
